package svg.hw.quiz;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

import svg.hw.recog.ImageData;

public class QuizCanvas extends JPanel {

    /**
     * The points that the user was drawing at.
     */
    protected Vector points;


    public QuizCanvas() {
        points = new Vector();
        addMouseListener(new MouseHandler());
        addMouseMotionListener(new MouseMotionHandler());
        setBackground(Color.white);
        setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
    }

    /**
     * Called to clear the image.
     */
    public void clear() {
        points.clear();
        this.getGraphics().setColor(Color.white);
        this.getGraphics().fillRect(0, 0, getWidth(), getHeight());
        repaint();
    }

    /**
     * This method is called to get the pixels of the
     * user drawn character. The white spaces are
     * removed automatically.
     *
     * @return ImageData pixels without whitespaces.
     */
    public ImageData getPixels(int downSampleWidth, int downSampleHeight) {
        int r = 0;
        int b = 0;

        // find the bottom-right corner of the drawn image
        for (int i = 0; i < points.size(); i++) {
            Point p = (Point)points.get(i);
            r = Math.max(p.x, r);
            b = Math.max(p.y, b);
        }

        int l = r;
        int t = b;

        // find the top-left corner of the drawn image
        for (int i = 0; i < points.size(); i++) {
            Point p = (Point)points.get(i);
            l = Math.min(p.x, l);
            t = Math.min(p.y, t);
        }

        int w = r - l;// + 1;
        int h = b - t;// + 1;

        float ratio = 1.5f;
        int unit_width = w / downSampleWidth;
        int unit_height = h / downSampleHeight;
        if (unit_height > ratio * unit_width) { // tall and skinny
            int extra_width = (int) ( (unit_height / ratio - unit_width) *
                                     downSampleWidth / 2 + 0.5);
            l = l - extra_width;
            r = r + extra_width;
        }
        else if (unit_width > ratio * unit_height) { // short and fat
            int extra_height = (int) ( (unit_width / ratio - unit_height) *
                                      downSampleHeight / 2 + 0.5);
            t = t - extra_height;
            b = b + extra_height;
        }

        w = r - l + 1;
        h = b - t + 1;

        ImageData data = new ImageData(w,h);
        for (int i = 0; i < points.size(); i++) {
            Point p = (Point)points.get(i);
            int x = p.x - l;
            int y = p.y - t;
            data.setData(x, y, 1);
        }

        Graphics g = getGraphics();
        g.setColor(Color.red);
        g.drawRect(l,t,w,h);
        g.dispose();

        //System.out.print(data);
        //System.out.println("l=" + l+" w="+w+" t="+t+" h="+h);

        return data;
    }

    class MouseHandler extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            points.add(e.getPoint());
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                clear();
            }
        }
    }

    class MouseMotionHandler extends MouseMotionAdapter {
        public void mouseDragged(MouseEvent e) {
            Point p = (Point)points.lastElement();
            Graphics2D g = (Graphics2D)getGraphics();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                               RenderingHints.VALUE_ANTIALIAS_ON);
            g.setStroke(new BasicStroke(2));
            g.drawLine(p.x, p.y, e.getX(), e.getY());
            points.add(e.getPoint());
            g.dispose();
        }
    }
}
